package com.link2room.rodimus.controller.manager;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import com.link2room.rodimus.dto.param.manager.SalesmanLeadingParamDto;
import com.link2room.rodimus.dto.param.manager.SalesmanManagerParamDto;
import com.link2room.rodimus.dto.result.base.SalesmanBaseDto;
import com.link2room.rodimus.dto.result.base.SalesmanIncomeDetailDto;
import com.link2room.rodimus.dto.result.base.SalesmanMemberDto;
import com.link2room.rodimus.dto.result.manager.IncomdetailSchedulesDto;
import com.link2room.rodimus.facade.ISalesmaManagerFacadeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.link2room.rodimus.constant.PermissionConstant.*;

/**
 * Created by VegetaKo on 2016/10/11.
 */
@Api(tags = "分销员相关")
@RestController
@RequestMapping("/manager/Salesmans")
public class SalesmanManagerController {
    @Resource
    private ISalesmaManagerFacadeService salesmaManagerFacadeService;

    @ApiOperation(value = "分销员列表",response = SalesmanBaseDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始时间", required = false, paramType = "query"),
            @ApiImplicitParam(name = "end", value = "结束时间", required = false, paramType = "query"),
            @ApiImplicitParam(name = "lhotelCodes", value = "酒店代码", required = false, paramType = "query"),
            @ApiImplicitParam(name = "salesmanNames", value = "分销员姓名", required = false, paramType = "query"),
            @ApiImplicitParam(name = "sta", value = "状态", required = false, paramType = "query"),
    })
    @RequestMapping(method = RequestMethod.GET)
    @L2RPermissionResources(value = saleManList, name = "全部分销员列表")
    public BaseResultDto GetSalesmanList(
            @RequestParam(name = "start", required = false) String start,
            @RequestParam(name = "end", required = false) String end,
            @RequestParam(name = "lhotelCodes", required = false) String lhotelCodes,
            @RequestParam(name = "salesmanNames", required = false) String salesmanNames,
            @RequestParam(name = "currentPage", required = false) Integer currentPage,
            @RequestParam(name = "pageSize", required = false) Integer pageSize,
            @RequestParam(name = "sta", required = true) String sta
    ) throws Exception {
        BaseResultDto res = new BaseResultDto();
        SalesmanManagerParamDto salesmanManagerParamDto = new SalesmanManagerParamDto();
        salesmanManagerParamDto.setStartDatetime(start);
        salesmanManagerParamDto.setEndDatetime(end);
        salesmanManagerParamDto.setSta(sta);
        salesmanManagerParamDto.setCurrentPage(currentPage);
        salesmanManagerParamDto.setPageSize(pageSize);
        salesmanManagerParamDto.setLhotelCode(lhotelCodes);
        salesmanManagerParamDto.setSalesmanName(salesmanNames);
        res.setResultCode(0);
        res.setQueryResult(salesmaManagerFacadeService.listSalesman(salesmanManagerParamDto));
        return res;
    }

    @RequestMapping(value = "/{salesmanNo}", method = RequestMethod.GET)
    @L2RPermissionResources(value = saleManInfo, name = "分销员详情")
    public BaseResultDto GetSalesman(
            @PathVariable String salesmanNo
    ) throws Exception {
        BaseResultDto res = new BaseResultDto();
        SalesmanManagerParamDto salesmanManagerParamDto = new SalesmanManagerParamDto();
        salesmanManagerParamDto.setSalesmanNo(salesmanNo);
        res.setResultCode(0);
        res.setResultInfo(salesmaManagerFacadeService.getSalesmanBySalesman(salesmanManagerParamDto));
        return res;
    }

    @ApiOperation(value = "分销员审核")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "salesmanNo", value = "分销员编码", required = true, paramType = "path"),
            @ApiImplicitParam(name = "SalesmanManagerParamDto", value = "salesmanManagerParamDto", required = true, paramType = "body"),
    })
    @RequestMapping(value = "/{salesmanNo}", method = RequestMethod.PATCH)
    @L2RPermissionResources(value = saleManAudit, name = "分销员审核")
    public BaseResultDto auditSalesman(
            @RequestBody SalesmanManagerParamDto salesmanManagerParamDto,
            @PathVariable String salesmanNo
    ) throws Exception {
        BaseResultDto res = new BaseResultDto();
        //SalesmanApiParamDto salesmanApiParamDto = new SalesmanApiParamDto();
        salesmanManagerParamDto.setSalesmanNo(salesmanNo);
        res.setResultCode(0);
        salesmaManagerFacadeService.auditSalesman(salesmanManagerParamDto);
        return res;
    }

    @ApiOperation(value = "分销员更新")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "salesmanNo", value = "分销员编码", required = true, paramType = "path"),
            @ApiImplicitParam(name = "SalesmanManagerParamDto", value = "salesmanManagerParamDto", required = true, paramType = "body"),
    })
    @RequestMapping(value = "/{salesmanNo}", method = RequestMethod.PUT)
    @L2RPermissionResources(value = updateSaleManInfo, name = "更新分销员详情")
    public BaseResultDto updateSalesman(
            @RequestBody SalesmanManagerParamDto salesmanManagerParamDto,
            @PathVariable String salesmanNo
    ) throws Exception {
        BaseResultDto res = new BaseResultDto();
        //SalesmanApiParamDto salesmanApiParamDto = new SalesmanApiParamDto();
        salesmanManagerParamDto.setSalesmanNo(salesmanNo);
        res.setResultCode(0);
        salesmaManagerFacadeService.modifySalesman(salesmanManagerParamDto);
        return res;
    }

    @ApiOperation(value = "分销员发展会员列表",response = SalesmanMemberDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "salesmanNo", value = "分销员编码", required = false, paramType = "path"),
    })
    @RequestMapping(value = "/{salesmanNo}/Members", method = RequestMethod.GET)
    @L2RPermissionResources(value = saleManMemberBySalemanNo, name = "分销员粉丝列表BySalemanNo")
    public BaseResultDto GetMemberListBySalesman(
            @PathVariable String salesmanNo
    ) throws Exception {
        BaseResultDto res = new BaseResultDto();
        SalesmanManagerParamDto salesmanManagerParamDto = new SalesmanManagerParamDto();
        salesmanManagerParamDto.setSalesmanNo(salesmanNo);
        res.setResultCode(0);
        res.setQueryResult(salesmaManagerFacadeService.listSalesmanMemberBySalesman(salesmanManagerParamDto));
        return res;
    }

    @ApiOperation(value = "分销员奖励详情",response = SalesmanIncomeDetailDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "salesmanNo", value = "分销员编码", required = false, paramType = "path"),
    })
    @RequestMapping(value = "/{salesmanNo}/Incomedetails", method = RequestMethod.GET)
    @L2RPermissionResources(value = saleManIncomedetails, name = "分销员收入列表")
    public BaseResultDto GetIncomeDetailsListBySalesman(
            @PathVariable String salesmanNo
    ) throws Exception {
        BaseResultDto res = new BaseResultDto();
        SalesmanManagerParamDto salesmanManagerParamDto = new SalesmanManagerParamDto();
        salesmanManagerParamDto.setSalesmanNo(salesmanNo);
        res.setResultCode(0);
        res.setQueryResult(salesmaManagerFacadeService.listSalesmanIncomeDetailBySalesman(salesmanManagerParamDto));
        return res;
    }

    @ApiOperation(value = "有效分销员列表",response = SalesmanBaseDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始时间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "end", value = "结束时间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "lhotelCodes", value = "酒店代码", required = false, paramType = "query"),
            @ApiImplicitParam(name = "salesmanNames", value = "分销员姓名", required = false, paramType = "query"),
    })
    @RequestMapping(value = "/Applications", method = RequestMethod.GET)
    @L2RPermissionResources(value = saleManApplications, name = "分销员列表")
    public BaseResultDto GetSalesmanApplicationList(
            @RequestParam(name = "start", required = true) String start,
            @RequestParam(name = "end", required = true) String end,
            @RequestParam(name = "lhotelCodes", required = false) String lhotelCodes,
            @RequestParam(name = "salesmanNames", required = false) String salesmanNames
    ) throws Exception {
        BaseResultDto res = new BaseResultDto();
        SalesmanManagerParamDto salesmanManagerParamDto = new SalesmanManagerParamDto();
        salesmanManagerParamDto.setLhotelCode(lhotelCodes);
        salesmanManagerParamDto.setSalesmanName(salesmanNames);
        salesmanManagerParamDto.setStartDatetime(start);
        salesmanManagerParamDto.setEndDatetime(end);
        salesmanManagerParamDto.setSta("R");
        res.setResultCode(0);
        res.setQueryResult(salesmaManagerFacadeService.listSalesman(salesmanManagerParamDto));
        return res;
    }

    @ApiOperation(value = "分销员会员列表",response = SalesmanMemberDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始时间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "end", value = "结束时间", required = true, paramType = "query")
    })
    @RequestMapping(value = "/Members", method = RequestMethod.GET)
    @L2RPermissionResources(value = saleManMembers, name = "分销员粉丝列表")
    public BaseResultDto GetSalesmanMemberList(
            @RequestParam(name = "start", required = true) String start,
            @RequestParam(name = "end", required = true) String end
    ) throws Exception {
        BaseResultDto res = new BaseResultDto();
        SalesmanManagerParamDto salesmanManagerParamDto = new SalesmanManagerParamDto();
        salesmanManagerParamDto.setStartDatetime(start);
        salesmanManagerParamDto.setEndDatetime(end);
        salesmanManagerParamDto.setSta("R");
        res.setResultCode(0);
        res.setQueryResult(salesmaManagerFacadeService.listSalesmanMember(salesmanManagerParamDto));
        return res;
    }

    @ApiOperation(value = "分销员奖励详情列表",response = SalesmanIncomeDetailDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "start", value = "开始时间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "end", value = "结束时间", required = true, paramType = "query"),
            @ApiImplicitParam(name = "awardSta", value = "奖励状态", required = true, paramType = "query"),
    })
    @RequestMapping(value = "/Incomedetails", method = RequestMethod.GET)
    @L2RPermissionResources(value = incomedetails, name = "分销收入详情")
    public BaseResultDto GetSalesmanIncomeDetailList(
            @RequestParam(name = "start", required = true) String start,
            @RequestParam(name = "end", required = true) String end,
            @RequestParam(name = "awardSta", required = false) String awardSta

    ) throws Exception {
        BaseResultDto res = new BaseResultDto();
        SalesmanManagerParamDto salesmanManagerParamDto = new SalesmanManagerParamDto();
        salesmanManagerParamDto.setStartDatetime(start);
        salesmanManagerParamDto.setEndDatetime(end);
        salesmanManagerParamDto.setAwardSta(awardSta);
        res.setResultCode(0);
        res.setQueryResult(salesmaManagerFacadeService.listSalesmanIncomeDetail(salesmanManagerParamDto));
        return res;
    }

    @ApiOperation(value = "分销员奖励审核")
    @ApiImplicitParams(value = {
    })
    @RequestMapping(value = "/Incomedetails/Auiteds", method = RequestMethod.POST)
    @L2RPermissionResources(value = incomedetailsAuiteds, name = "分销奖励审核")
    public BaseResultDto auditSalesmanIncomeDetailList() throws Exception {
        BaseResultDto res = new BaseResultDto();
        res.setResultCode(0);
        salesmaManagerFacadeService.auditSalesmanIncomeDetail();
        return res;
    }

    @ApiOperation(value = "分销员奖励发放")
    @ApiImplicitParams(value = {
    })
    @RequestMapping(value = "/Incomedetails/Paids", method = RequestMethod.POST)
    @L2RPermissionResources(value = incomedetailsPaids, name = "分销奖励发放")
    public BaseResultDto paySalesmanIncomeDetailList(
    ) throws Exception {

        BaseParamDto paramDto = new BaseParamDto();
        IncomdetailSchedulesDto schedules = salesmaManagerFacadeService.incomdetailSchedules(paramDto);
        BaseResultDto res = new BaseResultDto();
        res.setResultCode(0);
        if(DefaultValue.T.equals(schedules.getIsWorking())){
            res.setResultInfo(schedules);
        }else{
            salesmaManagerFacadeService.startPaySalesmanIncomeDetail();
        }
        return res;
    }

    /**
     * 奖励发放进度
     *
     * @return
     */
    @ApiOperation(value = "分销员奖励发放进度")
    @ApiImplicitParams(value = {
    })
    @RequestMapping(value = "/Incomedetails/Schedules", method = RequestMethod.GET)
    @L2RPermissionResources(value = incomedetailsSchedules, name="分销奖励发放进度")
    public BaseResultDto pidsSchedules() throws Exception {
        BaseParamDto paramDto = new BaseParamDto();
        IncomdetailSchedulesDto res = salesmaManagerFacadeService.incomdetailSchedules(paramDto);
        BaseResultDto dto = new BaseResultDto();
        dto.setResultCode(0);
        dto.setResultInfo(res);
        return dto;
    }

    @RequestMapping(path = "/salesmanSource/Leadin",method = RequestMethod.GET)
    public BaseResultDto salesmanSourceLeadin() throws Exception {
        SalesmanLeadingParamDto paramDto = new SalesmanLeadingParamDto();
        salesmaManagerFacadeService.salesmanLeading(paramDto);
        BaseResultDto dto = new BaseResultDto();
        dto.setResultCode(0);
        return dto;
    }
}
